#!/bin/bash
#参数1：环境，vagrant、fz、demo2
#参数2：tomcat存放cwebtools的目录路径

envTag=$1
tomcatWebappsDir=$2

if [ ! -n "$envTag" ] ;then
	envTag=vagrant
fi

if [ ! -n "$tomcatWebappsDir" ] ;then
	tomcatWebappsDir=/usr/share/tomcat
fi

sh createDB_cwebtools.sh

tarFileDir=/tmp/phantomonline

tar xzf ${tarFileDir}/cwebtools.tar.gz -C ${tarFileDir}
tar xzf ${tarFileDir}/pdf2word_fontdata.tar.gz -C ${tarFileDir}

result=$(cat ${tomcatWebappsDir}/conf/server.xml | grep "${tomcatWebappsDir}/webapps/cwebtools/pdftools")
if [[ "$result" == "" ]]
then
	lineNo=`sed -n -e '/<\/Host>/=' ${tomcatWebappsDir}/conf/server.xml`
	sed -i "${lineNo}i <Context docBase=\"${tomcatWebappsDir}/webapps/cwebtools/pdftools\" path=\"\" reloadable=\"true\" debug=\"0\"/>" ${tomcatWebappsDir}/conf/server.xml
fi

rm -rf ${tomcatWebappsDir}/webapps/cwebtools
mv ${tarFileDir}/cwebtools ${tomcatWebappsDir}/webapps/
chmod -R 777 ${tomcatWebappsDir}/webapps/cwebtools

if [ ! -f "${tomcatWebappsDir}/lib/mysql-connector-java-5.1.18-bin.jar" ]; then
	mv ${tarFileDir}/mysql-connector-java-5.1.18-bin.jar ${tomcatWebappsDir}/lib
fi

if [ ! -d "/usr/lib/pdf2word_fontdata" ]; then
	mv ${tarFileDir}/pdf2word_fontdata /usr/lib/
	chmod -R 777 /usr/lib/pdf2word_fontdata
fi

rm -rf ${tarFileDir}/mysql-connector-java-5.1.18-bin.jar ${tarFileDir}/pdf2word_fontdata

if [ ! -f "/lib64/libcwebtool_linux64.so" ]; then
	ln -s ${tomcatWebappsDir}/webapps/cwebtools/libcwebtool_linux64.so /lib64/libcwebtool_linux64.so
	ln -s ${tomcatWebappsDir}/webapps/cwebtools/libfauto_linux64.so /lib64/libfauto_linux64.so
	ln -s ${tomcatWebappsDir}/webapps/cwebtools/libicudata.so.55 /lib64/libicudata.so.55
	ln -s ${tomcatWebappsDir}/webapps/cwebtools/libicui18n.so.55 /lib64/libicui18n.so.55
	ln -s ${tomcatWebappsDir}/webapps/cwebtools/libicuuc.so.55 /lib64/libicuuc.so.55
fi

log4jConfigPath=${tomcatWebappsDir}/webapps/cwebtools/pdftools/WEB-INF/classes/log4j.properties
pathConfigPath=${tomcatWebappsDir}/webapps/cwebtools/pdftools/WEB-INF/classes/path.properties
sed -i 's/D:\/cwebtools/\/usr\/share\/tomcat\/webapps\/cwebtools/g' ${log4jConfigPath}
sed -i 's/D:\/cwebtools/\/usr\/share\/tomcat\/webapps\/cwebtools/g' ${pathConfigPath}
sed -i 's/D:\//\/home\//g' ${pathConfigPath}

lineNo=`sed -n -e '/^url_cpdfapi_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_cpdfapi_fz=https://www-${envTag}.connectedpdf.com" ${pathConfigPath}

lineNo=`sed -n -e '/^url_reset_password_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_reset_password_fz=https://www-${envTag}.connectedpdf.com/site/forgetpassword" ${pathConfigPath}

lineNo=`sed -n -e '/^url_current_user_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_current_user_fz=http://platform.${envTag}.foxitcloud.com:8003/rest/users/current" ${pathConfigPath}

lineNo=`sed -n -e '/^url_webpdf_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_webpdf_fz=http://phantom-${envTag}.connectedpdf.com:8890/" ${pathConfigPath}

lineNo=`sed -n -e '/^url_database_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_database_fz=jdbc:mysql://127.0.0.1/" ${pathConfigPath}

lineNo=`sed -n -e '/^url_appstore_fz=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i url_appstore_fz=https://cloud-${envTag}.connectedpdf.com/" ${pathConfigPath}

lineNo=`sed -n -e '/^database_user=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i database_user=root" ${pathConfigPath}

lineNo=`sed -n -e '/^database_pwd=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i database_pwd=" ${pathConfigPath}

lineNo=`sed -n -e '/^libPath=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i libPath=${tomcatWebappsDir}/webapps/cwebtools/" ${pathConfigPath}

lineNo=`sed -n -e '/^dllName=/=' ${pathConfigPath}`
sed -i "$lineNo d" ${pathConfigPath}
sed -i "$lineNo i dllName=libcwebtool_linux64.so" ${pathConfigPath}

service tomcat restart

echo "--> install cwebtools success, http://192.168.65.10:8180/"